//
//  SHMyViewController.m
//  UI_CarHome
//
//  Created by dllo on 15/10/21.
//  Copyright (c) 2015年 沈浩. All rights reserved.
//

#import "SHMyViewController.h"
#import "SHHandle.h"
#import "SHCollectionViewController.h"


@interface SHMyViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)UIImageView *imageView;
@property(nonatomic, retain)UILabel *label;
@property(nonatomic, retain)NSArray *bigArr;
@property(nonatomic, assign)CGFloat fileSize;
@property(nonatomic, assign)CGFloat folderSize;

@end

@implementation SHMyViewController

- (void)dealloc
{
    [_bigArr release];
    [_label release];
    [_imageView release];
    [_tableView release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的";
    self.view.backgroundColor = BACKGROUNDCOLOR;
    [self.tableView reloadData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isNight:) name:@"isNight" object:nil];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)style:UITableViewStylePlain];
    self.tableView.backgroundColor = BACKGROUNDCOLOR;
    self.tableView.separatorStyle = SEPARATORSTYLE;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    [self.tableView registerClass:[SHMyTableCell class] forCellReuseIdentifier:@"reuse"];
    NSArray *myArr = @[@"我的收藏"];
    NSArray *setArr = @[@"清除缓存", @"模式设置", @"版本检测"];
    self.bigArr = @[myArr, setArr];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    self.tableView.tableHeaderView = imageView;
    imageView.image = [UIImage imageNamed:@"car.jpg"];

}

- (void)isNight:(NSNotification *)notification{
    [self.tableView reloadData];
    self.tableView.backgroundColor = BACKGROUNDCOLOR;
    self.view.backgroundColor = BACKGROUNDCOLOR;
    self.tableView.separatorStyle = SEPARATORSTYLE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        SHCollectionViewController *collection = [[SHCollectionViewController alloc] init];
        [self.navigationController pushViewController:collection animated:YES];
        [collection release];

    } else if (indexPath.section == 1 && indexPath.row == 0) {
        // 找到cache文件路径
        NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        
        float size = 0.0;
        size = [self folderSizeAtPath:cachePath];
    
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"当前缓存大小为:" message:[NSString stringWithFormat:@"%.2fM", size] delegate:nil cancelButtonTitle:@"清除" otherButtonTitles:@"取消", nil];
        alertView.delegate = self;
        [alertView show];
    } else if (indexPath.section == 1 && indexPath.row == 2){
       
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic objectForKey:kCFBundleVersionKey];
       
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前版本" message:currentVersion delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        alert.tag = 1000;
    }
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1000) {
        
    } else {
    
        if(buttonIndex == 0) {
            NSFileManager *manager = [NSFileManager defaultManager];
            
            NSArray *cacheBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *cacheBoxPath = cacheBox[0];
            BOOL result = [manager removeItemAtPath:cacheBoxPath error:nil];
            if (result) {
                NSLog(@"删除成功");
            } else {
                NSLog(@"删除失败");
            }
        }
    }
}

#pragma mark --单个文件的大小
- (long long)fileSizeAtPath:(NSString *)filePath {
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]) {
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

#pragma mark --遍历文件夹获得文件夹大小,返回多少MB

- (float)folderSizeAtPath:(NSString *)folderPath {
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) {
        return 0;
    }
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString *fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil) {
        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize / (1024.0 * 1024.0);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SHMyTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *array = self.bigArr[indexPath.section];
    cell.myLabel.text = array[indexPath.row];
    cell.indexPath = indexPath;
    cell.myLabel.textColor = TEXTCOLOR;
    cell.detaLabel.textColor = TEXTCOLOR;
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = self.bigArr[section];
    return array.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.bigArr.count;
}

#pragma mark 高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
